package org.iartisan.maven.plugin.utils;

import java.io.*;

/**
 * <p>
 * 文件相关帮助类
 *
 * @author King
 * @since 2017/9/11
 */
public class FileUtil {

    private final static String DEFAULT_CHARSET = "UTF-8";

    public static void writeToFile(String fileName, String filePath, String content) throws IOException {
        File file = new File(new StringBuffer(filePath).append(File.separator).append(fileName).toString());
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), DEFAULT_CHARSET));
        bw.write(content);
        bw.flush();
        bw.close();
    }

    public static String getFilePath(String directory, String _package) {
        File folder = new File(directory + File.separator + getPackagePath(_package));
        if (!folder.exists()) {
            folder.mkdirs();
        }
        return folder.getPath();
    }

    public static String getPackagePath(String _package) {
        if (StringUtil.isEmpty(_package)) {
            return "";
        }
        return _package.replace(".", File.separator);
    }
}
